<template>
	<view>
		<!-- App.vue在uni-app中主要用于全局样式和生命周期，不用于页面内容显示 -->
	</view>
</template>

<script setup>
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import { isLoggedIn, STORAGE_KEYS, shouldWxAuth, hasToken } from './common/auth'
import config from './common/config'
import { userApi } from './common/api'
import { startNotificationService, stopNotificationService } from './common/notification'

onLaunch(() => {
	// 如果要区分开发和生产环境
	// process.env.NODE_ENV值为development或production
	if (process.env.NODE_ENV === 'development') {
		console.log('开发环境');
		config.baseUrl = 'http://127.0.0.1:7000/mx'
		config.mapKey = '5L4BZ-E3P3P-A2QDC-LFIK6-SR6JJ-XUFTX'
	} else {
		console.log('生产环境');
		config.baseUrl = 'https://mx.119xfq.cn/mx'
		config.mapKey = '5L4BZ-E3P3P-A2QDC-LFIK6-SR6JJ-XUFTX'
	}
})

onShow(() => {
	console.log('App Show', isLoggedIn())
	if (hasToken()) {
		userApi.getUserInfo().then(res => {
			if (res.success) {
				uni.setStorageSync(STORAGE_KEYS.USER_INFO, res.data)
				// 启动通知服务，检查待办任务
				startNotificationService()
			}
			else {
				uni.removeStorageSync(STORAGE_KEYS.USER_INFO)
				uni.removeStorageSync(STORAGE_KEYS.USER_TOKEN)
				uni.reLaunch({
					url: '/pages/home/home'
				})
			}
		}).catch(error => {
			console.warn('获取用户信息失败:', error)
		})
	}
})

onHide(() => {
	console.log('App Hide')
	// 停止通知服务，节省资源
	stopNotificationService()
})

</script>

<style>
/*每个页面公共css */

/* H5特定样式 */
/* #ifdef H5 */
body {
	margin: 0;
	padding: 0;
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
}

/* 防止登录框显示时页面滚动 */
body.modal-open {
	overflow: hidden;
}

/* #endif */
</style>
